﻿using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.EventBus;

namespace System 
{

    public class UIEventArgs
    {
        public object Data { get; set; }
        public string Type { get; set; }
    }
    public delegate void UIEventHandler(object sender, UIEventArgs e);
    public static   class UIEventBus // : IUIEventBus
    {
        private static Dictionary<string, List< UIEventHandler>> handlers = new Dictionary<string, List< UIEventHandler>>();  
        public static void Emit(string key, object sender, UIEventArgs args)
        {
            if (handlers.ContainsKey(key))
            {
                handlers[key].ForEach(h => { h(sender,args); });
            }
        }
        public static void On(string key,  UIEventHandler handler)
        {
            if (!handlers.ContainsKey(key))
                handlers[key] = new List<UIEventHandler>();
            handlers[key].Add(handler);
            Debug(key);
        }
        public static void Off(string key)
        {
   
            if (handlers.ContainsKey(key))
                handlers.Remove(key);
            Debug(key);
        }
        public static string GetKey(string moudel,string method)
        {
            string key= $"{moudel}:{method}";
            Debug(key);
            return key;
        }
        private static void Debug(string key)
        {
#if DEBUG
            if (handlers.ContainsKey(key) && handlers[key].Count > 1)
            {
                Console.WriteLine($"请注意，该key{key}成了多播事件,确认是否要多播，不需要多播，请换其他key！！");
            }
#endif
        }
    }
   
}
